package com.jianjian.test;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;
import java.util.Properties;
import java.util.ResourceBundle;
import com.jianjian.test.*;

/**
 * @author Zsir
 * @create 2021-12-12 14:48
 */
public class testDruid {

    @Test
    public void test1() throws SQLException {
    //创建数据源
        DruidDataSource dataSource = new DruidDataSource();
        //设置数据库连接参数
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("jian");
        //获取连接对象
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
    }

    //读取jdbc配置文件创建数据源
    @Test
    public void test2() throws SQLException {
        //加载类路径下的jdbc.properties
        ResourceBundle rb = ResourceBundle.getBundle("jdbc");
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(rb.getString("jdbc.Driver"));
        dataSource.setUrl(rb.getString("jdbc.url"));
        dataSource.setUsername(rb.getString("jdbc.username"));
        dataSource.setPassword(rb.getString("jdbc.password"));
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);

    }
}
